package com.fourth_groups.shopping121store.ui.sight.adapter;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.fourth_groups.shopping121store.bean.SightBean;
import com.fourth_groups.shopping121store.ui.sight.activity.WebActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/10.
 */
public class AutoShowImgPagerAdapter extends PagerAdapter{

    private List<ImageView> images = new ArrayList<>();
    List<SightBean.DataBean.BlockBean> mBlocks;


    public AutoShowImgPagerAdapter(List<ImageView> images, List<SightBean.DataBean.BlockBean> blocks) {
        this.images= images;
        this.mBlocks=blocks;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        container.addView(images.get(position % images.size()),0);

//            if (position % images.size() == images.size() - 1) {
                View view = images.get(position % images.size());
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(container.getContext(), WebActivity.class);
                        assert mBlocks!=null;
                        intent.putExtra("urlImg",mBlocks.get(0).getItems().get(position% images.size()).getUrl());

                        container.getContext().startActivity(intent);

                    }
                });
//            }

            return images.get(position % images.size());

    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //从容器中移除某个条目
        container.removeView(images.get(position % images.size()));
    }
}
